package com.icss.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.icss.model.ItemCategory;
import com.icss.service.ItemCategoryService;
import com.icss.service.ItemService;
@Controller
@RequestMapping("icc")
public class ItemCategoryController {
	@Autowired
	ItemCategoryService ics;
	//类别管理首页//分页
	@RequestMapping("itemCategory")
	public String itemCategory(Model model,@RequestParam(value="current",defaultValue="1")int current) {
		List<ItemCategory> listAll = ics.findAll();
		int totalPage=0;//总页数
		int pageSize=5;//每页显示的记录数
		if(listAll.size()%pageSize==0) {
			totalPage=listAll.size()/pageSize;
		}else {
			totalPage=listAll.size()/pageSize+1;
		}
		int startIndex = 0;
		startIndex = (current-1)*pageSize;
		Map<String, Integer> map=new HashMap<String,Integer>();
		map.put("start", startIndex);
		map.put("pageSize", pageSize);
		List<ItemCategory> findPage = ics.findByPage(map);
		model.addAttribute("page", findPage);
		model.addAttribute("totalPage",totalPage);
		model.addAttribute("current",current);
		return "itemCategory/itemCategory";
	}
	//类别管理添加页面
	@RequestMapping("addCategory")
	public String addCategory() {
		return "itemCategory/add";
	}
	//提交添加类别
	@RequestMapping("toAdd")
	public String toAdd(ItemCategory i) {
		boolean add = ics.add(i);
		if(add) {
			return "redirect:/icc/itemCategory";
		}else {
			return "redirect:/icc/addCategory";
		}
	}
	@RequestMapping("delete")
	public String delete(int id) {
		boolean del = ics.del(id);
		if(del) {
			return "redirect:/icc/itemCategory";
		}else {
			return "redirect:/icc/itemCategory";
		}
	}
	@RequestMapping("update")
	public String update(int id,Map<String, Object>map) {
		ItemCategory findById = ics.findById(id);
		map.put("findById", findById);
		return "itemCategory/update";
	}
	@RequestMapping("toUpdate")
	public String toUpdate(ItemCategory i) {
		boolean update = ics.update(i);
		if(update)  {
			return "redirect:/icc/itemCategory";
		}else {
			return "redirect:/icc/addCategory";
		}
	}
}
